package com.ngsky.ana.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static SimpleDateFormat DateTimeFormatSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    public static SimpleDateFormat DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat MonthFormat = new SimpleDateFormat("yyyy-MM");
    public static String DATE_FORMAT = "yyyy年MM月dd日";

    public static String formatDate(Date date){
        return DateFormat.format(date);
    }

    public static String formatDate(Date date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String formatMonth(Date date){
        return MonthFormat.format(date);
    }

    public static String formatDateTime(Date date){
        return DateTimeFormat.format(date);
    }
    public static String formatDateTimeSSS(Date date){
        return DateTimeFormatSSS.format(date);
    }

    public static String formatDateString(String date) {
        return formatDate(parseDate(date), DATE_FORMAT);
    }

    public static Date parseMonth(String dateStr){
        if(dateStr==null || "".equals(dateStr)){
            return null;
        }
        Date date = null;
        try {
            date = MonthFormat.parse(dateStr);
        }catch (ParseException e){
        }
        return date;
    }

    public static Date parseDate(String dateStr){
        if(dateStr==null || "".equals(dateStr)){
            return null;
        }
        Date date = null;
        try {
            date = DateFormat.parse(dateStr);
        }catch (ParseException e){
        }
        return date;
    }

    public static Date parseDateTime(String dateStr){
        if(dateStr==null || "".equals(dateStr)){
            return null;
        }
        Date date = null;
        try {
            date = DateTimeFormat.parse(dateStr);
        }catch (ParseException e){
        }
        return date;
    }

    public static Date dateAddHours(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hours);
        return calendar.getTime();
    }

    public static Date dateAddDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    public static Date dateAddMinutes(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

    public static Date dateMonthBefor(Date date, int i){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -i);
        return calendar.getTime();
    }

    public static boolean isMonth(String month) {
        try {
            MonthFormat.parse(month);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static String timeBetweet(Date start, Date end){
        Long s = Math.abs(start.getTime()-end.getTime())/1000;
        if(s<60){
            return s + "s";
        }
        Long m = s/60;
        if(m<60){
            return m + "m";
        }
        Long h = m/60;
        if(h<24){
            return h + "h";
        }
        Long d = h/24;
        return d + "d";
    }

    /**
     * 获取日期的年份。失败返回0。
     *
     * @param date 日期
     * @return 年份
     */
    public static int getYear(Date date) {
        return getInteger(date, Calendar.YEAR);
    }

    /**
     * 获取日期的月份。失败返回0。
     *
     * @param date 日期
     * @return 月份
     */
    public static int getMonth(Date date) {
        return getInteger(date, Calendar.MONTH) + 1;
    }

    /**
     * 获取日期的天数。失败返回0。
     *
     * @param date 日期
     * @return 天
     */
    public static int getDay(Date date) {
        return getInteger(date, Calendar.DATE);
    }

    /**
     * 获取日期中的某数值。如获取月份
     *
     * @param date     日期
     * @param dateType 日期格式
     * @return 数值
     */
    private static int getInteger(Date date, int dateType) {
        int num = 0;
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
            num = calendar.get(dateType);
        }
        return num;
    }

}
